package com.cskaoyan.service.admin.impl;

import com.cskaoyan.bean.MarketCategory;
import com.cskaoyan.bean.MarketCategoryExample;
import com.cskaoyan.bean.param.CommonData;
import com.cskaoyan.bean.vo.category.AdminCategorySimpleInfoVo;
import com.cskaoyan.bean.vo.category.AdminCategoryVo;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.cskaoyan.service.admin.AdminCategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author :rain
 * @description 要根据对应的level来区分类目的等级，pid区分一级类目和从属的二级类目
 * @since 2022/06/26 22:19
 */
@Service
public class AdminCategoryServiceImpl implements AdminCategoryService {

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    /**
     * 查询所有类目
     *
     * @return {@link CommonData }<{@link AdminCategoryVo }>
     * @author: jinmo
     * @date: 2022/06/27 15:50:15
     */
    @Override
    public CommonData<AdminCategoryVo> queryAllList() {
        //获得l1类目个数
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andLevelEqualTo("L1").andDeletedEqualTo(false);
        int limit = (int) marketCategoryMapper.countByExample(marketCategoryExample);
        PageHelper.startPage(1, limit);
        //获取商品类目等级为一的类目
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        //装换对象,并查询子集合添加到对象
        List<AdminCategoryVo> adminCategoryVos = new ArrayList<>();
        for (MarketCategory marketCategory : marketCategories) {
            AdminCategoryVo adminCategoryVo = AdminCategoryVo.convertMarketCategoryToAdminCategoryVo(marketCategory);
            //获取对应的子类目（父id为对象id）
            MarketCategoryExample categoryExample = new MarketCategoryExample();
            MarketCategoryExample.Criteria exampleCriteria = categoryExample.createCriteria();
            exampleCriteria.andPidEqualTo(adminCategoryVo.getId()).andDeletedEqualTo(false);
            List<MarketCategory> categories = marketCategoryMapper.selectByExample(categoryExample);
            //装换集合中为AdminCategoryVo集合
            List<AdminCategoryVo> adminCategoryVosL2 = new ArrayList<>();
            for (MarketCategory category : categories) {
                adminCategoryVosL2.add(AdminCategoryVo.convertMarketCategoryToAdminCategoryVo(category));
            }
            //添加集合为父对象children属性
            adminCategoryVo.setChildren(adminCategoryVosL2);
            //添加最后对象到集合
            adminCategoryVos.add(adminCategoryVo);
        }

        PageInfo<AdminCategoryVo> adminCategoryVoPageInfo = new PageInfo<>(adminCategoryVos);
        return CommonData.data(adminCategoryVoPageInfo);
    }

    /**
     * 查询l1类别的简单信息
     *
     * @return {@link CommonData }<{@link AdminCategorySimpleInfoVo }>
     * @author: jinmo
     * @date: 2022/06/27 16:36:14
     */
    @Override
    public CommonData<AdminCategorySimpleInfoVo> queryL1CategorySimpleInfo() {
        //获得l1类目个数
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andLevelEqualTo("L1").andDeletedEqualTo(false);
        int limit = (int) marketCategoryMapper.countByExample(marketCategoryExample);
        PageHelper.startPage(1, limit);
        //获取商品类目等级为一的类目
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        //装换对象
        List<AdminCategorySimpleInfoVo> adminCategorySimpleInfoVos = new ArrayList<>();
        for (MarketCategory marketCategory : marketCategories) {
            AdminCategorySimpleInfoVo adminCategorySimpleInfoVo = new AdminCategorySimpleInfoVo();
            adminCategorySimpleInfoVo.setValue(marketCategory.getId());
            adminCategorySimpleInfoVo.setLabel(marketCategory.getName());
            adminCategorySimpleInfoVos.add(adminCategorySimpleInfoVo);
        }
        //返回
        PageInfo<AdminCategorySimpleInfoVo> adminCategorySimpleInfoVoPageInfo = new PageInfo<>(adminCategorySimpleInfoVos);
        return CommonData.data(adminCategorySimpleInfoVoPageInfo);
    }

    /**
     * 新建一个商品类目
     *
     * @param marketCategory 市场分类
     * @return {@link CommonData }<{@link MarketCategory }>
     * @author: jinmo
     * @date: 2022/06/27 17:02:08
     */
    @Override
    public MarketCategory create(MarketCategory marketCategory) {
        marketCategory.setName(marketCategory.getName().trim());
        //给对象添加添加时间和更新时间
        Date date = new Date();
        marketCategory.setAddTime(date);
        marketCategory.setUpdateTime(date);
        //插入数据库，同时给对象id属性赋值
        marketCategoryMapper.insertReturnId(marketCategory);

        return marketCategory;
    }

    /**
     * 更新商品类别
     *
     * @param adminCategoryVo
     * @author: jinmo
     * @date: 2022/06/27 18:02:55
     */
    @Override
    public void update(AdminCategoryVo adminCategoryVo) {
        //转换对象
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setId(adminCategoryVo.getId());
        marketCategory.setName(adminCategoryVo.getName().trim());
        marketCategory.setKeywords(adminCategoryVo.getKeywords());
        marketCategory.setDesc(adminCategoryVo.getDesc());
        marketCategory.setIconUrl(adminCategoryVo.getIconUrl());
        marketCategory.setPicUrl(adminCategoryVo.getPicUrl());
        marketCategory.setLevel(adminCategoryVo.getLevel());
        marketCategory.setPid(adminCategoryVo.getPid());
        //更新数据库
        marketCategoryMapper.updateByPrimaryKeySelective(marketCategory);
    }

    /**
     * 删除对应类目和子类目下的类目
     *
     * @param marketCategory 市场分类
     * @author: jinmo
     * @date: 2022/06/27 18:25:47
     */
    @Override
    public void delete(MarketCategory marketCategory) {
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();

        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andPidEqualTo(marketCategory.getId());
        marketCategoryExample.or().andIdEqualTo(marketCategory.getId());
        //新建一个临时对象用于删除
        MarketCategory marketCategoryTemp = new MarketCategory();
        marketCategoryTemp.setDeleted(true);
        marketCategoryMapper.updateByExampleSelective(marketCategoryTemp, marketCategoryExample);
    }


    // @Override
    // public CommonData<AdminCategoryVo> queryAllList() {
    //     //开启分页
    //     PageHelper.startPage(2, 2);
    //     List<AdminCategoryVo> list = new ArrayList<>();
    //     //首先得获取商品类目等级为一的商品
    //     MarketCategory marketCategory = new MarketCategory();
    //     marketCategory.setLevel("L1");
    //     //获取到商品类目为1的所有种类
    //     List<MarketCategory> levelOne = marketCategoryMapper.selectAllLevelOne(marketCategory);
    //
    //     //遍历所有的等级为1的类目，获取类目一的pid，根据类目一的pid获取类目二的信息
    //     List<AdminCategoryChildrenVo> childrenVoArrayList = new ArrayList<>();
    //     for (int j = 0; j < levelOne.size(); j++) {
    //         //获取当前levelOne的pid
    //         Integer levelOnePid = levelOne.get(j).getPid();
    //         //根据商品类目为1的Pid查询二级类目
    //         childrenVoArrayList = marketCategoryMapper.selectAllLevelTwo(levelOnePid);
    //         //封装返回对象
    //         AdminCategoryVo adminCategoryVo = new AdminCategoryVo();
    //         adminCategoryVo.getAdminCatoryInfo(levelOne.get(j).getId(), levelOne.get(j).getName(),
    //                                            levelOne.get(j).getKeywords(), levelOne.get(j).getDesc(),
    //                                            levelOne.get(j).getIconUrl(), levelOne.get(j).getPicUrl(),
    //                                            levelOne.get(j).getLevel());
    //         //封装children
    //         adminCategoryVo.setChildren(childrenVoArrayList);
    //         //将当前对象放入对应的list中
    //         list.add(adminCategoryVo);
    //     }
    //     PageInfo<AdminCategoryVo> pageInfo = new PageInfo<>(list);
    //     return CommonData.data(pageInfo);
    // }
}
